﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using Controller.DataProvider;
using System.Data;
using System.Collections.ObjectModel;
using ManageStore.IViews;
using BaseUtilities;

namespace Controller
{
    public class SupplierCtr : BaseCtr, IDataErrorInfo
    {
        public SupplierCtr(ISupplierView pISupplierView)
        {
            this.ISupplierView = pISupplierView;
        }

        #region Properties
        // ISupplierView.
        public ISupplierView ISupplierView { get; set; }
        // SupplierPro.
        private SupplierPro _SupplierPro;
        public SupplierPro SupplierPro
        {
            get
            {
                if (_SupplierPro == null)
                {
                    _SupplierPro = new SupplierPro();
                }
                return _SupplierPro;
            }
            set { _SupplierPro = value; }
        }
        // Supplier.
        private Supplier _Supplier;
        public Supplier Supplier
        {
            get
            {
                if (_Supplier == null)
                {
                    _Supplier = new Supplier();
                }
                return _Supplier;
            }
            set
            {
                if (_Supplier != value)
                {
                    _Supplier = value;
                    OnPropertyChanged("Supplier");
                }
            }
        }
        // LstErrorFields
        private Dictionary<string, string> _DicError;
        public Dictionary<string, string> DicError
        {
            get
            {
                if (_DicError == null)
                    _DicError = new Dictionary<string, string>();
                return _DicError;
            }
            set
            {
                _DicError = value;
            }
        }
        #endregion Properties

        #region Binding properties
        // Id
        public int Id
        {
            get
            {
                return Supplier.Id;
            }
            set
            {
                if (Supplier.Id != value)
                {
                    Supplier.Id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        // SupplierName
        public string SupplierName
        {
            get
            {
                return Supplier.Name;
            }
            set
            {
                if (Supplier.Name != value)
                {
                    Supplier.Name = value;
                    OnPropertyChanged("SupplierName");
                }
            }
        }
        // Address
        public string Address
        {
            get
            {
                return Supplier.Address;
            }
            set
            {
                if (Supplier.Address != value)
                {
                    Supplier.Address = value;
                    OnPropertyChanged("Address");
                }
            }
        }
        // Contact
        public string Contact
        {
            get
            {
                return Supplier.Contact;
            }
            set
            {
                if (Supplier.Contact != value)
                {
                    Supplier.Contact = value;
                    OnPropertyChanged("Contact");
                }
            }
        }
        // Email
        public string Email
        {
            get
            {
                return Supplier.Email;
            }
            set
            {
                if (Supplier.Email != value)
                {
                    Supplier.Email = value;
                    OnPropertyChanged("Email");
                }
            }
        }
        // Fax
        public string Fax
        {
            get
            {
                return Supplier.Fax;
            }
            set
            {
                if (Supplier.Fax != value)
                {
                    Supplier.Fax = value;
                    OnPropertyChanged("Fax");
                }
            }
        }
        // MobilePhone
        public string MobilePhone
        {
            get
            {
                return Supplier.MobilePhone;
            }
            set
            {
                if (Supplier.MobilePhone != value)
                {
                    Supplier.MobilePhone = value;
                    OnPropertyChanged("MobilePhone");
                }
            }
        }
        // Note
        public string Note
        {
            get
            {
                return Supplier.Note;
            }
            set
            {
                if (Supplier.Note != value)
                {
                    Supplier.Note = value;
                    OnPropertyChanged("Note");
                }
            }
        }
        // Phone
        public string Phone
        {
            get
            {
                return Supplier.Phone;
            }
            set
            {
                if (Supplier.Phone != value)
                {
                    Supplier.Phone = value;
                    OnPropertyChanged("Phone");
                }
            }
        }
        // TaxCode
        public string TaxCode
        {
            get
            {
                return Supplier.TaxCode;
            }
            set
            {
                if (Supplier.TaxCode != value)
                {
                    Supplier.TaxCode = value;
                    OnPropertyChanged("TaxCode");
                }
            }
        }
        // LstSupplier.
        private ObservableCollection<Supplier> _LstSupplier;
        public ObservableCollection<Supplier> LstSupplier
        {
            get
            {
                if (_LstSupplier == null)
                {
                    _LstSupplier = new ObservableCollection<BusinessModel.Supplier>(GetAll().ToResult<IList<Supplier>>());
                }
                return _LstSupplier;
            }
            set
            {
                if (_LstSupplier != value)
                {
                    _LstSupplier = value;
                    OnPropertyChanged("LstSupplier");
                }
            }
        }

        #endregion Binding properties

        private void LoadListSupplier()
        {
            LstSupplier = new ObservableCollection<BusinessModel.Supplier>(GetAll().ToResult<IList<Supplier>>());
        }

        #region View Handlers
        //----------------------------------------
        public bool MtdNew(params object[] pParams)
        {
            bool result = false;
            if (ISupplierView.BeforeHandler("EvtNew"))
            {
                LoadListSupplier();
                ISupplierView.ReloadData();
                Supplier = new Supplier();
                DicError.Clear();
                ISupplierView.CompletedHandler("EvtNew");
            }
            return result;
        }
        //----------------------------------------
        public bool MtdEdit(params object[] pParams)
        {
            bool result = false;
            if (ISupplierView.BeforeHandler("EvtEdit"))
            {
                if (pParams.Count() == 1)
                {
                    Supplier = pParams[0] as Supplier;
                    if (Supplier == null)
                    {
                        ISupplierView.ErrorHandler("EvtEdit");
                    }
                    else
                    {
                        DicError.Clear();
                        ISupplierView.CompletedHandler("EvtEdit");
                    }
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdDelete(params object[] pParams)
        {
            bool result = false;
            if (ISupplierView.BeforeHandler("EvtDelete"))
            {
                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + SupplierPro.EntityName, "Id", Supplier.Id);
                ResultModel oResultModel = SupplierPro.DeleteById(oEntityKey);
                if (oResultModel.HasError)
                {
                    ISupplierView.ErrorHandler("EvtDelete", oResultModel);
                }
                else
                {
                    LstSupplier = new ObservableCollection<BusinessModel.Supplier>(GetAll().ToResult<IList<Supplier>>());
                    ISupplierView.CompletedHandler("EvtDelete");
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public bool MtdSave(params object[] pParams)
        {
            // Check validate.
            if (DicError.Count > 0)
            {
                ISupplierView.ErrorHandler("EvtSave", DicError);
                return false;
            }
            bool result = false;
            if (ISupplierView.BeforeHandler("EvtSave"))
            {
                if (Supplier.Id == default(int))
                {
                    Supplier.CreatedBy = ControllerConfig.Default.UserName;
                    Supplier.CreatedDate = DateTime.Now;
                }
                Supplier.ModifiedBy = ControllerConfig.Default.UserName;
                Supplier.ModifiedDate = DateTime.Now;

                EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + SupplierPro.EntityName, "Id", Supplier.Id);
                ResultModel oResultModel = SupplierPro.Save(Supplier, oEntityKey);
                if (oResultModel.HasError)
                {
                    ISupplierView.ErrorHandler("EvtSave", oResultModel);
                }
                else
                {
                    LstSupplier = new ObservableCollection<BusinessModel.Supplier>(GetAll().ToResult<IList<Supplier>>());
                    ISupplierView.CompletedHandler("EvtSave", oResultModel.Result);
                    result = true;
                }
            }
            return result;
        }
        //----------------------------------------
        public ResultModel GetById(int pSupplierId)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + SupplierPro.EntityName, "Id", pSupplierId);
            return SupplierPro.GetById(oEntityKey);
        }
        //----------------------------------------
        public ResultModel GetAll()
        {
            return SupplierPro.GetAll();
        }
        #endregion View Handlers

        #region IDataErrorInfo Members
        private string _Error;
        public string Error
        {
            get
            {
                return _Error;
            }
        }
        public string this[string columnName]
        {
            get
            {
                string error = string.Empty;
                if (DicError.Keys.Contains(columnName))
                    DicError.Remove(columnName);
                switch (columnName)
                {
                    case "SupplierName":
                        if (string.IsNullOrWhiteSpace(SupplierName))
                        {
                            error = "Tên hàng hóa không được rỗng.";
                        }
                        break;
                }
                _Error = error;
                if (!string.IsNullOrWhiteSpace(error))
                {
                    DicError.Add(columnName, error);
                }
                ISupplierView.ValidationHandler(columnName, error);
                return error;
            }
        }

        #endregion
    }
}
